#!/bin/bash
# This script does work currently needed to regenerate the sources found
# in Source/GeneratedServices.

set -eu

# Where does this script live so we can hit things in the tree.
readonly ToolsDir=$(dirname "$(echo $0 | sed -e "s,^\([^/]\),$(pwd)/\1,")")
readonly ScriptName=$(basename "$0")

readonly ServiceGeneratorDir="${ToolsDir}/ServiceGenerator"
readonly GeneratedServicesDir="${ToolsDir}/../GeneratedServices"
# This only works for Xcode default setting on where build output goes and when
# building with -alltargets and not with -scheme.
readonly ServiceGeneratorBinary="${ServiceGeneratorDir}/build/Debug/ServiceGenerator"

BRIEF_MODE=--brief
if [[ $# -ge 1 && ( "$1" == "--no-brief" ) ]] ; then
  BRIEF_MODE=
  shift
fi
readonly EXTRA_OPTS=$*

# -----------------------------------------------------------------------------

# Helper to print a message and hopefully have it seen.
Banner() {
  echo ""
  echo "==============================================================================="
  echo "    $*"
  echo "==============================================================================="
  echo ""
}

# -----------------------------------------------------------------------------

Banner "Ensuring ServiceGenerator is up to date..."
/usr/bin/xcodebuild \
  -project "${ServiceGeneratorDir}/ServiceGenerator.xcodeproj" \
  -configuration Debug \
  -alltargets \
  build

# -----------------------------------------------------------------------------

SERVICE_GENERATOR_COMMAND=(
  "${ServiceGeneratorBinary}"
    --outputDir "${GeneratedServicesDir}"
    --addServiceNameDir yes
    --removeUnknownFiles yes
    --auditJSON
    --guessFormattedNames
    --httpHeader X-User-IP:0.0.0.0
    ${BRIEF_MODE}
    ${EXTRA_OPTS}
    # Generate everything flagged as preferred.
    --generatePreferred
    # At least one server from preferred is returning content-type: text/html.
    --parseTextHTMLReplies
)

SERVICE_GENERATOR_COMMAND+=(
  # The Admin SDK is odd, it is a bunch of 'admin' apis with different versions
  # to "group" them (https://www.googleapis.com/discovery/v1/apis?name=admin);
  # so only one gets marked as preferred. To generate all of it we have to
  # manually fetch anything not preferred which means updating this if there are
  # changes. admin:reports_v1 is currently the preferred one in discovery.
  'https://admin.googleapis.com/$discovery/rest?version=datatransfer_v1'
  'https://admin.googleapis.com/$discovery/rest?version=directory_v1'
  
  # Not sure why this is in discovery, turn it off.
  --skip prod_tt_sasportal

  # Include a filter so there are no warnings/infos during normal generation.
  --messageFilter "${ToolsDir}/${ScriptName}-message_filter.json"
)

# Away it goes...
Banner "Running the ServiceGenerator..."
"${SERVICE_GENERATOR_COMMAND[@]}"

# -----------------------------------------------------------------------------

Banner "Ensure the podspec is up to date..."
"${ToolsDir}/update_podspec_services.py"

# -----------------------------------------------------------------------------

Banner "Ensure Package.swift is up to date..."
"${ToolsDir}/update_swiftpm_services.py"
